package com.example.smallmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.dto.SpOrderQueryParam;
import com.example.smallmall.entity.SpOrder;
import com.example.smallmall.entity.SpOrderGoods;
import com.example.smallmall.service.ISpOrderGoodsService;
import com.example.smallmall.service.ISpOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author WSGL
 * @since 2020-09-17
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class SpOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SpOrderController.class);

    @Resource
    private ISpOrderService iSpOrderService;
    @Resource
    private ISpOrderGoodsService iSpOrderGoodsService;

    @ApiOperation("订单管理列表")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<Map<String, Object>> listOrder(SpOrderQueryParam queryParam,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") @ApiParam("页码") Integer pageNum,
                                                       @RequestParam(value = "pageSize") @ApiParam("每页数量") Integer pageSize) {
        Map<String, Object> objectMap = iSpOrderService.selectByPage(queryParam, pageNum, pageSize);
        if (objectMap != null) {
            return CommonResult.success(objectMap);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("查看订单详情")
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult<Map<String, Object>> findById(@PathVariable("id") Integer orderId) {
        LambdaQueryWrapper<SpOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpOrder::getOrderId, orderId);
        Map<String, Object> map = iSpOrderService.getMap(lambdaQueryWrapper);
        if (map != null && !map.isEmpty()) {
            LambdaQueryWrapper<SpOrderGoods> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(SpOrderGoods::getOrderId, orderId);
            List<Map<String, Object>> listMaps = iSpOrderGoodsService.listMaps(lambdaQueryWrapper1);
            if (listMaps != null && !listMaps.isEmpty()) {
                map.put("goods", listMaps);
            }
            return CommonResult.success(map);
        } else {
            return CommonResult.failed("操作失败");
        }
    }
}
